package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.common.exception.CustomException;
import com.heima.feign.article.IArticleClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.statistics.dto.NewsDimensionDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.dtos.GraphicDataParametersDto;
import com.heima.model.wemedia.pojos.ImageAndTextData;
import com.heima.model.wemedia.pojos.NewPage;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.DateUtils;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.service.WmStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 许敦军
 * @version 1.0
 * @project heima-leadnews
 * @date 2024/1/26 22:42:09
 */
@Service
public class WmStatisticsServiceImpl implements WmStatisticsService {
    @Autowired
    private IArticleClient iArticleClient;

    /**
     * 查询图文数据统计
     *
     * @param newsDimensionDto 新闻维度dto
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult newsDimension(NewsDimensionDto newsDimensionDto) {
        if (newsDimensionDto.getBeginDate() == null && newsDimensionDto.getEndDate() == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        Date beginDateTime = DateUtils.stringToDate(newsDimensionDto.getBeginDate(), "yyyy-MM-dd");
        Date endDateTime = DateUtils.stringToDate(newsDimensionDto.getEndDate(), "yyyy-MM-dd");
        // 获取当前的登陆用户的id
        WmUser user = WmThreadLocalUtil.getUser();
        GraphicDataParametersDto graphicDataParametersDto = new GraphicDataParametersDto();
        graphicDataParametersDto.setId(user.getId());
        graphicDataParametersDto.setEndDateTime(endDateTime);
        graphicDataParametersDto.setBeginDateTime(beginDateTime);
        //
        ResponseResult responseResult = iArticleClient.graphicAndTextualData(graphicDataParametersDto);
        if (responseResult.getCode().equals(200)) {
            String data = JSON.toJSONString(responseResult.getData());
            ImageAndTextData imageAndTextData = JSON.parseObject(data, ImageAndTextData.class);
            return ResponseResult.okResult(imageAndTextData);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    /**
     * 查看图文数据列表
     *
     * @param newPageDto 新建页面
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult newPage(NewPage newPageDto) {
        //参数校验
        if(newPageDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取当前登陆用户的id
        WmUser user = WmThreadLocalUtil.getUser();
        newPageDto.setUserId(user.getId());
        //开启远程调用
        PageResponseResult PageResponseResult = iArticleClient.newPage(newPageDto);
        return PageResponseResult;
    }


}
